package com.itany.corejava.code16_反射;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年12月18日 16:43
 */
public class Test06_解析注解 {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        Class c = Student.class;

        //1.获取类上的注解
        Annotation[] annotations = c.getAnnotations();
        System.out.println(Arrays.toString(annotations));

        // 2.获取类上指定的注解
        Annotation annotation = c.getAnnotation(SomeAnnotation.class);
        System.out.println(annotation);
        annotation=c.getAnnotation(OtherAnnotation.class);
        System.out.println(annotation);
        System.out.println("------------------------------");

        // 3.获取属性上的注解
        Field nameField=c.getDeclaredField("name");
        Annotation[] annotations2=nameField.getAnnotations();
        System.out.println(Arrays.toString(annotations2));

        //4.获取属性上的指定注解
        Annotation annotation2=nameField.getAnnotation(SomeAnnotation.class);
        System.out.println(annotation2);

        // 5.获取方法上所有注解
        Method method=c.getDeclaredMethod("show");
        Annotation[] annotations3=method.getAnnotations();
        System.out.println(Arrays.toString(annotations3));

        // 6.获取方法上指定注解
        Annotation annotation4=method.getAnnotation(OtherAnnotation.class);
        System.out.println(annotation4);

        // 7.获取指定注解中指定属性的值
        System.out.println(annotation4 instanceof  OtherAnnotation);
        System.out.println(annotation4 instanceof  SomeAnnotation);
        if(annotation4 instanceof  OtherAnnotation){
            OtherAnnotation otherAnnotation= (OtherAnnotation) annotation4;
            System.out.println("encoding:"+otherAnnotation.encoding());
            System.out.println("method:"+Arrays.toString(otherAnnotation.method()));
        }
        Annotation annotation5=method.getAnnotation(SomeAnnotation.class);
        System.out.println(annotation5);
        if(annotation5 instanceof  SomeAnnotation){
            SomeAnnotation someAnnotation= (SomeAnnotation) annotation5;
            System.out.println("name:"+someAnnotation.name());
            System.out.println("value:"+someAnnotation.value());
            System.out.println("age:"+someAnnotation.age());
        }


        //8.判断调用者是否使用了指定注解
        // 在开发中，某些注解可能没有在指定的元素上使用
        // 如果直接解析，可能会报错
        // 因此需要进行判断，判断调用者是否使用了注解

        Field ageField=c.getDeclaredField("age");
        System.out.println(ageField.isAnnotationPresent(SomeAnnotation.class));
        if(ageField.isAnnotationPresent(SomeAnnotation.class)){
            SomeAnnotation someAnnotation= ageField.getAnnotation(SomeAnnotation.class);
            System.out.println("name:"+someAnnotation.name());
            System.out.println("value:"+someAnnotation.value());
            System.out.println("age:"+someAnnotation.age());
        }else{
            System.out.println("age属性中没有使用SomeAnnotation注解");
        }

    }
}
